/**
 * 公共事件
 */
import {Util} from "@lowcode/jpaas-base-lib";
import DrillDownView from "@/views/modules/preview/components/common/drill-down-view.vue";

export default {
    data() {
        return {
        }
    },
    created() {
        let self_=this;
        this.$bus.on('chartEvent',  (args)=> {
            self_.handReceive(args,self_)
        });
        this.$bus.on('filterEvent',  (args)=> {
            self_.handReceive(args,self_)
        });
    },
	beforeDestroy() {
		//销毁事件
		this.$bus.off("chartEvent");
		this.$bus.off("filterEvent");
	},
    methods:{
		i18n(name,text,key){
			return this.$ti18(name,text,"previewLang",key);
		},
        /**
         * 获取筛选器组件参数值
         */
        getFilterParams(){
            let params="";
            //所有组件
            if(! this.screenConfig){
                return ;
            }
            let allComponents = this.screenConfig.components;
            let filters=allComponents.filter(item=>{
                if(item.type== "filter"){
                    return true;
                }
            });
            for (let i = 0; i < filters.length; i++) {
                let filter= filters[i];
                if(filter.config && filter.config.advancedConf && filter.config.advancedConf.publish){
                    let paramMappings=filter.config.advancedConf.publish.paramMapping;
                    if(!paramMappings){
                        return;
                    }
                    for (let j = 0; j < paramMappings.length; j++) {
                        let paramMapping=paramMappings[j];
                        if(!params){
                            params={}
                        }
                        if ((paramMapping.type == 'user' || paramMapping.type == 'group') && paramMapping.values) {
                            var ary = [];
                            for (let i = 0; i < paramMapping.values.length; i++) {
                                ary.push(paramMapping.values[i].id);
                            }
                            paramMapping.value = ary.join(",");
                        } else if (paramMapping.type == 'tree' && paramMapping.values) {
                            paramMapping.value = paramMapping.values.join(",");
                        }
                        params[paramMapping.key] = paramMapping.value || "";
                    }
                }
            }
            return params;
        },
		//下钻
		drillDown(curConfig,drillDownConfig,params){
			let parentViews=JSON.parse(JSON.stringify(this.parentViews))
        	if(!drillDownConfig){
        		return;
			}
			let drillDownConf=JSON.parse(JSON.stringify(drillDownConfig))
        	//下钻响应的组件
        	let resConfig=""
			for (let i = 0; i < drillDownConf.configs.length; i++) {
				let conf=drillDownConf.configs[i];
				if(curConfig.key==conf.parentKey){
					resConfig=JSON.parse(JSON.stringify(conf));
					break;
				}
			}
			if(!resConfig.field){
				return;
			}
			let drillDownValue="";
			if(this.data[params.dataIndex]){
				drillDownValue=this.data[params.dataIndex][resConfig.field];
			}else {
				drillDownValue=params.name;
			}
			//下钻父节点
			if(!parentViews || parentViews.length==0){
				parentViews=[{
					value:this.i18n("all","全部"),
					key:resConfig.parentKey
				}];
			}
			var parentView = parentViews.find(item => item.key == resConfig.key);
			if(!parentView){
				parentViews.push({
					value:drillDownValue,
					key:resConfig.key
				});
			}
			let self=this;
			//展示方式
			if(resConfig.showType=='current'){
				if(this.config.chartType=="map"){
					this.setParentViews(parentViews);
				}else {
					this.$emit("setDrillDownValue",drillDownValue);
					this.$emit("setParentViews",parentViews);
				}
                resConfig.config.advancedConf["drillDownConfig"]=drillDownConf;
				//取上层的标题配置
				resConfig.config.titleStyle=this.config.titleStyle;
				resConfig.config.showTitle=this.config.showTitle;
				resConfig.config.title=this.config.title;
				//设置当前配置内容
                this.config=resConfig.config;
                if(this.config.chartType=="map"){
                    this.mapTypeDown = drillDownValue
                }
                if( this.config.dataSources.filterParams && this.config.dataSources.filterParams.length>0){
                    let filterParam=this.config.dataSources.filterParams[0];
                    this.filterParams.push({key: filterParam.tableName+"."+filterParam.fieldName, value: drillDownValue, opType: "EQ"});
                }
			}else {
				let  obj={
					component: DrillDownView,
					curVm: this,
					title:this.config.title,
					zIndex: 10000,
					data:{
						config:resConfig.config,
						screenConfig:this.screenConfig,
						drillDownValue:drillDownValue,
						drillDownConfig:drillDownConf
					},
					cancel:function (){
						let idx=-1;
						for (let i = 0; i < parentViews.length; i++) {
							if(parentViews[i].key==resConfig.key){
								idx=i;
								break;
							}
						}
						if(idx!=-1){
							parentViews.splice(idx,parentViews.length-idx)
							self.$emit("setParentViews",parentViews);
						}
					}
				}
				if(resConfig.isMax=="NO"){
					let width=resConfig.width?resConfig.width+"px":"800px";
					let height=resConfig.height?resConfig.height+"px":"600px";
					obj.widthHeight= [width, height];
				}else {
					obj.max=true;
				}
				Util.open(obj, function (action) {
					if (action != 'ok') return;
				});
			}
		},
		//跳转图表
		selectView(view,index){
			this.filterParams=[];
			if(!this.screenConfig){
			    return;
            }
			if(this.config.key!=view.key){
				let config=this.getConfig(this.screenConfig.components,view.key);
				if(view.value==this.i18n("all","全部")){
					if(this.config.chartType=="map"){
						this.setParentViews([]);
					}else {
						this.$emit("setParentViews",[]);
					}
				}else {
					if( this.config.dataSources.filterParams && this.config.dataSources.filterParams.length>0){
						let filterParam=this.config.dataSources.filterParams[0];
						this.filterParams.push({key: filterParam.tableName+"."+filterParam.fieldName, value: view.value, opType: "EQ"});
					}
					let parentViews=JSON.parse(JSON.stringify(this.parentViews));
					parentViews.splice(index+1,parentViews.length-index+1);
					if(this.config.chartType=="map"){
						this.setParentViews(parentViews);
					}else {
						this.$emit("setParentViews",parentViews);
					}
				}
                this.config=config;
			}
		},
		getConfig(components,key){
        	let obj="";
			components.filter(item=>{
				if(item.config && item.config.key==key){
					obj=item.config;
					return true;
				}else if(item.config.advancedConf && item.config.advancedConf.drillDownConfig){
					let configs=item.config.advancedConf.drillDownConfig.configs;
					for (let i = 0; i < configs.length; i++) {
						if(configs[i].config && configs[i].config.key==key){
							obj=configs[i].config;
							return true;
						}
					}
				}
			})
			return obj;
		}
    }
}
